#!/bin/sh
# postinst script for hustoj
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

DB_USER=`cat /etc/mysql/debian.cnf |grep user|head -1|awk  '{print $3}'`
DB_PASSWORD=`cat /etc/mysql/debian.cnf |grep password|head -1|awk  '{print $3}'`
HUSTOJ_ROOT='/home/judge'
PHP_VER=`apt-cache search php-fpm|grep -e '[[:digit:]]\.[[:digit:]]' -o`
case "$1" in
    configure)
    /etc/init.d/mysql restart
    /etc/init.d/php$PHP_VER-fpm restart
    mysql -h localhost -u$DB_USER -p$DB_PASSWORD < $HUSTOJ_ROOT/sql/db.sql
    ln -s /home/judge/etc/nginx.conf /etc/nginx/conf.d/hustoj.conf
    sed -i "s/OJ_USER_NAME=root/OJ_USER_NAME=$DB_USER/g" $HUSTOJ_ROOT/etc/judge.conf
    sed -i "s/OJ_PASSWORD=root/OJ_PASSWORD=$DB_PASSWORD/g" $HUSTOJ_ROOT/etc/judge.conf
    sed -i "s/DB_USER=\"root\"/DB_USER=\"$DB_USER\"/g" $HUSTOJ_ROOT/src/web/include/db_info.inc.php
    sed -i "s/DB_PASS=\"root\"/DB_PASS=\"$DB_PASSWORD\"/g" $HUSTOJ_ROOT/src/web/include/db_info.inc.php
    chmod 775 -R /home/judge/data && chgrp -R www-data /home/judge/
    chown www-data:www-data -R /home/judge/src/web/

echo "insert into jol.privilege values('admin','administrator','true','N');"|mysql jol
echo "insert into jol.privilege values('admin','source_browser','true','N');"|mysql jol

if grep "added by hustoj" /etc/nginx/sites-enabled/default ; then
        echo "default site modified!"
else
        echo "modify the default site"
        sed -i "s#root /var/www/html;#root /home/judge/src/web;#g" /etc/nginx/sites-enabled/default
        sed -i "s:index index.html:index index.php:g" /etc/nginx/sites-enabled/default
        sed -i "s:#location ~ \\\.php\\$:location ~ \\\.php\\$:g" /etc/nginx/sites-enabled/default
        sed -i "s:#\tinclude snippets:\tinclude snippets:g" /etc/nginx/sites-enabled/default
        sed -i "s|#\tfastcgi_pass unix|\tfastcgi_pass unix|g" /etc/nginx/sites-enabled/default
        sed -i "s:}#added by hustoj::g" /etc/nginx/sites-enabled/default
        sed -i "s:php7.4:php$PHP_VER:g" /etc/nginx/sites-enabled/default
        sed -i "s|# deny access to .htaccess files|}#added by hustoj\n\n\n\t# deny access to .htaccess files|g" /etc/nginx/sites-enabled/default
        sed -i "s|fastcgi_pass 127.0.0.1:9000;|fastcgi_pass 127.0.0.1:9000;\n\t\tfastcgi_buffer_size 256k;\n\t\tfastcgi_buffers 32 64k;|g" /etc/nginx/sites-enabled/default
fi
    /etc/init.d/nginx restart
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

# Automatically added by dh_installinit/13.6ubuntu1
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -z "${DPKG_ROOT:-}" ] && [ -x "/etc/init.d/hustoj" ]; then
		update-rc.d hustoj defaults >/dev/null
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		invoke-rc.d hustoj $_dh_action || exit 1
	fi
fi
# End automatically added section


exit 0
